package com.qianfeng.smsplatform.strategy.dfa;


import com.qianfeng.smsplatform.common.constants.CacheConstants;
import com.qianfeng.smsplatform.strategy.feign.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Component
public class SensitiveWordInit {


    private CacheService cacheService;

    @Autowired
    public void setCacheService(CacheService cacheService) {
        this.cacheService = cacheService;
    }

    /**
     * 字符编码
     */
    private static final String ENCODING = "utf8";
    /**
     * 敏感词库
     */
    @SuppressWarnings("rawtypes")
    private HashMap sensitiveWordMap;

    @SuppressWarnings("rawtypes")
    Map initKeyWord() {
        try {
            // 读取敏感词库
            Set<String> keyWordSet = readSensitiveWordFile();
            // 将敏感词库加入到HashMap中
            addSensitiveWordToHashMap(keyWordSet);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sensitiveWordMap;
    }

    /**
     * 读取敏感词库，将敏感词放入HashSet中，构建一个DFA算法模型：<br>
     * 中 = { isEnd = 0 国 = {<br>
     * isEnd = 1 人 = {isEnd = 0 民 = {isEnd = 1} } 男 = { isEnd = 0 人 = { isEnd =
     * 1 } } } } 五 = { isEnd = 0 星 = { isEnd = 0 红 = { isEnd = 0 旗 = { isEnd = 1
     * } } } }
     *
     * @param keyWordSet 敏感词库
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    private void addSensitiveWordToHashMap(Set<String> keyWordSet) {
        // 初始化敏感词容器，减少扩容操作
        sensitiveWordMap = new HashMap(keyWordSet.size());
        String key;
        Map nowMap;
        Map<String, String> newWordMap;
        // 迭代keyWordSet
        for (String aKeyWordSet : keyWordSet) {
            // 关键字
            key = aKeyWordSet;
            nowMap = sensitiveWordMap;
            for (int i = 0; i < key.length(); i++) {
                // 转换成char型
                char keyChar = key.charAt(i);
                // 65279是一个空格
                if ((int) keyChar == 65279) {
                    continue;
                }
                // 获取
                Object wordMap = nowMap.get(keyChar);

                if (wordMap != null) {
                    // 如果存在该key，直接赋值
                    nowMap = (Map) wordMap;
                } else {
                    // 不存在则，则构建一个map，同时将isEnd设置为0，因为他不是最后一个
                    newWordMap = new HashMap<>();
                    // 不是最后一个
                    newWordMap.put("isEnd", "0");
                    nowMap.put(keyChar, newWordMap);
                    nowMap = newWordMap;
                }

                if (i == key.length() - 1) {
                    // 最后一个
                    nowMap.put("isEnd", "1");
                }
            }
        }
    }

    /**
     * 读取敏感词库中的内容，将内容添加到set集合中
     *
     * @return Set
     * @throws Exception Exception
     */
    private Set<String> readSensitiveWordFile() throws Exception {
//        Set<String> dirtyWords = new HashSet<>();
//        dirtyWords.add("转生");
//        dirtyWords.add("贷款");
//        dirtyWords.add("利率");
//        dirtyWords.add("利息");
//        dirtyWords.add("返现");
//        dirtyWords.add("刷单");
//        dirtyWords.add("股票");
//        dirtyWords.add("保健");
//        dirtyWords.add("保健品");
//        dirtyWords.add("长生");
//        dirtyWords.add("转世");
//        dirtyWords.add("高利率");
//        dirtyWords.add("洗钱");
//
//        for (int i = 0; i < 17; i++) {
//            List<String> strings=new ArrayList<>();
//            for (String dirtyWord : dirtyWords) {
//                strings.add(dirtyWord + i);
//            }
//            dirtyWords.addAll(strings);
//        }
//        System.err.println("DFA方式敏感词数据长度"+dirtyWords.size());

        //初始化我们的敏感词,数据保存在 redis中
        Set<String> members = cacheService.members(CacheConstants.CACHE_DIRTY_KEY);
        return members;
    }
}
